package iosr11.Service;

import iosr11.contact.dao.ContactsHelper;
import iosr11.data.Attendence;
import iosr11.data.Event;
import iosr11.data.ProjectGroup;
import iosr11.data.Student;
import iosr11.gbase.dao.AttendenceDAO;
import iosr11.gbase.dao.EventDAO;
import iosr11.gbase.dao.ProjectGroupDAO;
import iosr11.gbase.dao.StudentDAO;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

public class AttendenceServiceImpl implements AttendenceService {

	private AttendenceDAO attendenceDAO;
	private EventDAO eventDAO;
	private StudentDAO studentDAO;
	private ContactsHelper contactsDAO;
	private ProjectGroupDAO projectGroupDAO;
	private Logger log = Logger.getLogger("service-log");
	
	
	
	@Override
	public Class<Attendence> getServiceEntityClass() {
		return Attendence.class;
	}

	@Override
	public Attendence getAttendence(String id) {
		Attendence attendence = attendenceDAO.getByID(id);
		System.out.println(attendence);
		if (attendence==null){
			log.info("brak rekordu o podanym id");
			return null;
		}
		if (attendence.getEvent()==null){
			log.warning("bledny rekord");
			return null;
		}
		if (attendence.getEvent().getId()==null){
			log.warning("bledny rekord");
			return null;
		}
		if (attendence.getStudent()==null){
			log.warning("bledny rekord");
			return null;
		}
		if (attendence.getStudent().getId()==null){
			log.warning("bledny rekord");
			return null;
		}
		Event event = eventDAO.getByID(attendence.getEvent().getId());
		Student student = studentDAO.getByID(attendence.getStudent().getId());
		attendence.setEvent(event);
		attendence.setStudent(student);
		return attendence;
	}

	@Override
	public boolean createAttendence(Student student, Event event, int pressence) {
		Attendence attendence = new Attendence();
		attendence.setStudent(student);
		attendence.setEvent(event);
		attendence.setPressence(pressence);
		return this.addAttendence(attendence);
	}

	@Override
	public boolean addAttendence(Attendence attendence) {
		log.info("Dodawanie obecnosci o wartosci: "+attendence.getPressence());
		return attendenceDAO.add(attendence);
	}

	@Override
	public List<Attendence> getAttendencesForEvent(Event event) {
		List<Attendence> attendences = attendenceDAO.getEventAttendence(event);
		if (attendences==null){
			log.info("brak obecnosci - null");
			return null;
		}
		if (attendences.size()<1){
			log.info("brak obecnosci - lista pusta");
			return attendences;
		}
		log.info("znaleziono obecnosci do eventu w ilosci: "+attendences.size());
		for (Attendence tmp : attendences){
			tmp.setEvent(event);
			if (tmp.getStudent()==null){
				log.warning("bledna obecnosc - przerwano - brak studenta");
				return null;
			}
			tmp.setStudent(studentDAO.getByID(tmp.getStudent().getId()));
			if (tmp.getStudent().getUser()==null){
				log.warning("bledna obecnosc - przerwano - brak usera dla studenta");
				return null;
			}
			tmp.getStudent().setUser(contactsDAO.getUserByRef(tmp.getStudent().getUser().getId()));
			if (tmp.getStudent().getProjectGrp()!=null){
				if (tmp.getStudent().getProjectGrp().getId()!=null){
					log.info("poszukuje grupy projektowej");
					ProjectGroup group = projectGroupDAO.getByID(tmp.getStudent().getProjectGrp().getId());
					tmp.getStudent().setProjectGrp(group);
				}
			}
		}
		SortResult.sortAttendences(attendences);
		event.setAttendences(attendences);
		return attendences;
	}

	@Override
	public List<Attendence> getAll() {
		List<Attendence> lista = attendenceDAO.getAll();
		if (lista==null){
			log.info("brak obecnosci");
			return null;
		}
		List<Attendence> result = new ArrayList<Attendence>();
		for(Attendence att : lista){
			Attendence tmp = this.getAttendence(att.getId());
			if (tmp!=null)
				result.add(tmp);
		}
		return result;
	}

	@Override
	public boolean deleteAttendence(Attendence attendence) {
		log.info("Usuwanie obecnosci: "+attendence.getId());
		return attendenceDAO.delete(attendence);
	}

	@Override
	public boolean updateAttendence(Attendence attendence) {
		log.info("Aktualizacja obecnosci o id: "+attendence.getId());
		return attendenceDAO.update(attendence);
	}

	public void setAttendenceDAO(AttendenceDAO attendenceDAO) {
		this.attendenceDAO = attendenceDAO;
	}

	public void setEventDAO(EventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}

	public void setStudentDAO(StudentDAO studentDAO) {
		this.studentDAO = studentDAO;
	}

	public void setContactsDAO(ContactsHelper contactsDAO) {
		System.out.println("dodaje kontakt");
		this.contactsDAO = contactsDAO;
	}

	public void setProjectGroupDAO(ProjectGroupDAO projectGroupDAO) {
		this.projectGroupDAO = projectGroupDAO;
	}
	
}
